package com.xs.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xs.entity.Person;
import com.xs.message.Sender;
import com.xs.message.ShopChannel;
import com.xs.simple.demo.MySource;

@RestController
public class DemoController {

	@Autowired
	private MySource mySource;
	
	
	

	@Resource(name = Sender.commentInfo)
	private MessageChannel commentChannel;

	@Resource(name = Sender.orderInfo)
	private MessageChannel orderChannel;

	@Resource(name = Sender.userInfo)
	private MessageChannel userChannel;

	@Resource(name = ShopChannel.SHOP_OUTPUT)
	private MessageChannel sendShopMessageChannel;

	@RequestMapping("/sendComment")
	public void sendComment(String message) {
		commentChannel.send(new GenericMessage<String>(message));
	}

	@RequestMapping("/sendUser")
	public void sendUser(String message) {
		userChannel.send(new GenericMessage<String>(message));
	}

	@RequestMapping("/sendOrder")
	public void sendOrder(String message) {
		orderChannel.send(new GenericMessage<String>(message));
	}

	@PostMapping("/sendMsg")
	public String sendShopMessage(Person content) {
		boolean isSendSuccess = sendShopMessageChannel.send(MessageBuilder.withPayload(content).build());
		return isSendSuccess ? "发送成功" : "发送失败";
	}

	@PostMapping("/mySourceSend")
	public String mySourceSend(String message) {
		boolean isSendSuccess = mySource.output().send(MessageBuilder.withPayload("hello 123").build());
		return isSendSuccess ? "发送成功" : "发送失败";
	}

}
